package ckw.database.model;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import ckw.managers.ModulesManager;

@Entity(name = "ckw.Voter")
public class Voter extends core.database.Voter<ElectionVoter> {
	private static final long serialVersionUID = -5241792427651766752L;
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "node", insertable = false, updatable = true)
	private Node node;
//	@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, mappedBy = "hasVoted", targetEntity = Election.class)
//	private List<Election> votedElections = new ArrayList<Election>();
//	@OneToMany
//	@JoinColumn(name = "voter")
//	private Set<ElectionVoter> elections = new HashSet<ElectionVoter>();
//	@OneToMany
//	@JoinColumn(name = "voter")
//	private Set<NonresidentVoter> nonresidentElections = new HashSet<NonresidentVoter>();
	
	public Node getNode() {
		return node;
	}

	public void setNode(Node node) {
		this.node = node;
		node.getVoters().add(this);
	}

//	public Set<ElectionVoter> getElections() {
//		return elections;
//	}
//
//	public void setElections(Set<ElectionVoter> elections) {
//		this.elections = elections;
//	}
//
//	public Set<NonresidentVoter> getNonresidentElections() {
//		return nonresidentElections;
//	}
//
//	public void setNonresidentElections(Set<NonresidentVoter> nonresidentElections) {
//		this.nonresidentElections = nonresidentElections;
//	}

	public void changeNode(Node node) {
		Node oldNode = this.getNode();
		if (oldNode != null) {
			oldNode.getVoters().remove(this);
			ModulesManager.getInstance().updateNode(oldNode);
		}
		this.setNode(node);
	}
	
	public void deleteNode() {
		if (this.node != null) {
			this.node.getVoters().remove(this);
			ModulesManager.getInstance().updateNode(this.node);
			node = null;
		}
	}
}
